

#version 120
//lightPerFrag.fp

uniform vec4 light_pos;
uniform sampler2D texMap;

varying vec4 emissive;
varying float shiny;
varying vec3 normal;
varying float d;
varying vec3 lightDir;
varying vec3 halfvector;
varying vec2 st;

varying vec4 ambientProduct;
varying vec4 diffuseProduct;
varying vec4 specularProduct;


const float constantAttn = 0.0;
const float linearAttn = 0.0;
const float quadraticAttn = 0.05;


float shininess = shiny;

void directionalLight(	in vec3 normal,
						inout vec4 ambient,
						inout vec4 diffuse,
						inout vec4 specular)
{
	vec3 VP;
	vec3 HV;
	float NdotVP;
	float NdotHV;
	float pf;	
	
	VP = normalize(light_pos.xyz);
	HV = normalize(halfvector);

	NdotVP = max(0.0, dot(normal, VP));
	NdotHV = max(0.0, dot(normal, HV));
	pf;
	
	if (NdotVP == 0.0)
		pf = 0.0;
	else
		pf = pow(NdotHV, shininess);
	
	ambient += ambientProduct;
	diffuse += diffuseProduct * NdotVP;
	specular += specularProduct * pf;
}

void main(void) {
	vec4 ambient = vec4(0.0);
	vec4 diffuse = vec4(0.0);
	vec4 specular = vec4(0.0);

	vec3 N = normalize(normal);
	
	directionalLight(N, ambient, diffuse, specular);
	
	
	if (st.st != vec2(0,0)){
		gl_FragColor = (ambient + diffuse + specular + emissive) * texture2D(texMap, st.st);
		//gl_FragColor = (ambient + diffuse + specular + emissive) * texture2D(texMap, gl_TexCoord[0].st);
		
	}else{
		gl_FragColor = (ambient + diffuse + specular + emissive);
	}
	//gl_FragColor = (ambient + diffuse + specular + emissive) * texture2D(texMap, gl_TexCoord[0]);


}
